# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2023/4/24 11:26
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : layer_6_transform_solution.py
# @Version: OpenPlanner2.0
import importlib

from OpenPlanner.core.src.op_class import OPClass
from OpenPlanner.debug.debug_utils import DEBUG_PRINT


class SolutionTransformingModule:
    def __init__(self,
                 op_obj: OPClass):
        self.op_obj = op_obj
        self.transform_hook_func = None

    def _window_transforming(self):
        pass

    def _frame_transforming(self):
        # TODO：
        # 1. 需要报文长度信息
        # 2. 需要时间粒度信息
        pass

    def transform_solution(self):
        strategy = self.op_obj.solution_transformation_option
        # print(strategy)

        if strategy == 'WINDOW':
            self._window_transforming()
        elif strategy == 'FRAME':
            self._frame_transforming()
        elif strategy == 'HOOK':
            base_module = \
                self.op_obj.output_transform_hook.base_module_path
            func_name = \
                self.op_obj.output_transform_hook.hook_func_name
            if base_module != '' or func_name != '':
                print("ERROR: solution_transform_option"
                      "填写HOOK选项时，必须实现钩子函数"
                      "对算法的解做转换")
                exit(0)
            pass
        elif strategy == 'NONE':
            pass
        else:
            print("ERROR: 不存在%s的转换选项"
                  "（solution_transform_option），"
                  "转换选项的可选值为"
                  "WINDOW、FRAME、HOOK和NONE。"
                  "请更新配置文件。" % strategy)
            exit(0)

    # 钩子函数相关
    def _register_transform_hook(self,
                                 func):
        self.transform_hook_func = func

    def custom_transforming_hook(self):
        base_module = \
            self.op_obj.output_transform_hook.base_module_path
        func_name = \
            self.op_obj.output_transform_hook.hook_func_name

        if base_module != '' and func_name != '':
            # 注册钩子
            # 判断模块是否存在
            try:
                module = importlib.import_module(base_module)
            except ModuleNotFoundError:
                print("ModuleNotFoundError:"
                      "结果转换钩子函数的%s模块不存在，"
                      "请检查钩子函数路径后更新配置文件" % base_module)
                exit(0)
            try:
                func = getattr(module, func_name)
            except AttributeError:
                print("AttributeError:"
                      "模块%s内没有函数%s，"
                      "请检查结果转换钩子函数名称（transform_hook"
                      "->hook_func_name）"
                      "后更新配置文件"
                      % (base_module, func_name))
                exit(0)
            self._register_transform_hook(func)

        # 执行钩子函数
        if self.transform_hook_func is not None:
            kwargs = self.op_obj.input_init_hook.hook_func_param
            self.transform_hook_func(self.op_obj, **kwargs)
        else:
            DEBUG_PRINT("No hook after solution transforming")
            pass
